<?php 
	/**
	 * 每天删除30天以前的数据
	 */

	include_once dirname(dirname(__FILE__))."/include/common.inc.php";
	include_once dirname(dirname(__FILE__))."/youyuan/function.youyuan.php";

	
	function getUser($ymd)
	{
		global $conn;
		$tblName = getTblName($ymd);
		$sql = "select  source_user_id,age,gender,has_photo from $tblName where fetch_date='$ymd' and `from`='youyuan' order by rand() limit 1000";
		$user = $conn->query($sql);
		$randuser = array();
		while ($row = $conn->fetch_assoc($user)) {
			if ($row['gender'] == 1) {
				$row['gender'] = '男';
			}else{
				$row['gender'] = '女';
			}
			if ($row['has_photo'] == 1) {
				$row['has_photo'] = '有照片';
			}else{
				$row['has_photo'] = '无照片';
			}
			$randuser[] = $row;
		}
		return $randuser;
	}
	//抓取日期，默认为昨天
	$ymd =  date("Y-m-d", time()-60*60*24);
	if ($argc > 1) {
		$ymd = date("Y-m-d", strtotime($argv[1]));
	}
	$file = 'user'.$ymd.'.txt';
	$page_dir = ROOT_PATH."/youyuan/pages";
	$page_dir = $page_dir."/".$ymd."/";
	$tblName = getTblName($ymd);
	$sql = "select  source_user_id,age,gender,has_photo from $tblName where fetch_date='$ymd' and `from`='youyuan'";
	$user = $conn->query($sql);
	$randuser = array();
	while ($row = $conn->fetch_assoc($user)) {
		$filename = $page_dir.$row['source_user_id'].'.html';
		$page = file_get_contents($filename);
		$page = str_replace(array(" ","\t","\n","\r","&nbsp;"), "", $page);
		preg_match('/<title>.*?-.*?-(.*?)-交友资料和照片/i', $page,$match);
		isset($match[1]) ? $nickname = $match[1] : $nickname = '未知';
		preg_match('/<divclass="section">(.*?)<divclass="heart_info">/i', $page, $matches);
		$tmp = $matches[1];
		preg_match('%收入：</label><spanclass="blue">(.*?)</span></li></ul></div>%i', $tmp,$income);
		if ($row['gender'] == 1) {
			$sex = '男';
		}else{
			$sex = '女';
		}
		$arr = array($row['source_user_id'],$nickname,$sex,$row['age'],$row['province'].$row['city']);
		$line = implode("\t", $arr);
		file_put_contents($file,$line."\n", FILE_APPEND);
	}
	
$value = 'youyuan';
$subject = '竞争对手'.$value.'数据提取';
$content = '竞争对手'.$value.'数据提取见附件';
$att = array($file);
sendMailEx($subject, $content,$att);

?>